package com.groupbuying.redpacket.service.impl;

import com.groupbuying.redpacket.context.FilterContextHandler;
import com.groupbuying.redpacket.dao.RedPacketListDao;
import com.groupbuying.redpacket.domain.RedPacketListDO;
import com.groupbuying.redpacket.service.RedPacketListService;
import com.groupbuying.redpacket.vo.RedPacketListVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service
public class RedPacketListServiceImpl implements RedPacketListService {
	private static final Logger logger = LoggerFactory.getLogger(RedPacketListServiceImpl.class);

	@Autowired
	private RedPacketListDao redPacketListDao;

	@Override
	public RedPacketListDO get(Long id){
		return redPacketListDao.get(id);
	}
	
	@Override
	public List<RedPacketListDO> list(Map<String, Object> map){
		return redPacketListDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return redPacketListDao.count(map);
	}
	
	@Override
	public int save(RedPacketListDO redPacketList){
		return redPacketListDao.save(redPacketList);
	}
	
	@Override
	public int update(RedPacketListDO redPacketList){
		return redPacketListDao.update(redPacketList);
	}
	
	@Override
	public int remove(Long id){
		return redPacketListDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return redPacketListDao.batchRemove(ids);
	}

	/**
	 * 根据taskId查询红包信息id[]
	 * @param taskId
	 * @return
	 */
	@Override
	public List<Long> listByTaskId(String taskId) {
		if (StringUtils.isEmpty(taskId)) {
			logger.error("红包信息,taskId不能为空或者不能为null", taskId);
			return null;
		}
		return redPacketListDao.listByTaskId(taskId);
	}

	/**
	 * 批量增加
	 * @param list
	 * @return
	 */
	@Override
	public int batchInsert (List<RedPacketListDO> list) {
		if (list.size() == 0 || list == null) {
			logger.error("红包信息,list不能为空或者不能为null", list);
			return 0;
		}
		return redPacketListDao.batchInsert(list);
	}

	/**
	 * 大屏幕WEB端轮次活动结束排行榜，当前分类当前轮次排行榜
	 * @param taskId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> currentRankingRotation(String taskId) {
		logger.info("获取taskId的值:{}", taskId);
		List<Map<String, Object>> mapList = new ArrayList<>();
		if (StringUtils.isEmpty(taskId)) {
			logger.error("红包信息,taskId不能为空或者不能为null", taskId);
			return mapList;
		}
		mapList = redPacketListDao.currentRankingRotation(taskId);
		return mapList;
	}

	/**
	 * 屏幕WEB端轮次活动结束排行榜，当前分类总排行榜
	 * @param platFrom
	 * @return
	 */
	@Override
	public List<Map<String, Object>> theTotalList(String platFrom) {
		logger.info("获取platFrom的值:{}", platFrom);
		List<Map<String, Object>> mapList = new ArrayList<>();
        if (StringUtils.isEmpty(platFrom)) {
            logger.error("红包信息,platFrom不能为空或者不能为null", platFrom);
            return mapList;
        }
        mapList = redPacketListDao.theTotalList(platFrom);
        return mapList;
	}

	/**
	 * 屏幕WEB端轮次活动结束排行榜，当前分类当前轮次排行榜标题
	 * @param taskId
	 * @param platFrom
	 * @return
	 */
	@Override
	public Map<String, Object> themeActivities(String taskId, String platFrom) {
		if (StringUtils.isEmpty(taskId)) {
			logger.error("红包信息,taskId不能为空或者不能为null", taskId);
			return null;
		}
		if (StringUtils.isEmpty(platFrom)) {
			logger.error("红包信息,platFrom不能为空或者不能为null", platFrom);
			return null;
		}
		return redPacketListDao.themeActivities(taskId, platFrom);
	}

	/**
	 * 微信小程序摇一摇抢红包中奖纪录，通过用户查询中奖纪录信息
	 * @param map
	 * @return
	 */
	@Override
	public List<RedPacketListVO> winningRecord(Map<String, Object> map) {
		String userId = FilterContextHandler.getUserID();
        logger.info("获取userId的值:{}", userId);
        if (StringUtils.isEmpty(userId)) {
            logger.error("红包信息,userId不能为空或者不能为null:{}", userId);
            return new ArrayList<>();
        }
		return redPacketListDao.winningRecord(map);
	}

	/**
	 * 微信小程序摇一摇抢红包中奖纪录，通过用户查询红包个数和红包总金额
	 * @param userId
	 * @return
	 */
	@Override
	public Map<String, Object> redPacketNumberMoney(String userId) {
		logger.info("获取userId的值:{}", userId);
		return redPacketListDao.redPacketNumberMoney(userId);
	}

	/**
	 * 微信小程序摇一摇抢红包中奖纪录，总条数
	 * @param map
	 * @return
	 */
	@Override
	public int winningRecordCount (Map<String, Object> map) {
		return redPacketListDao.winningRecordCount(map);
	}

	/**
	 * 大屏幕WEB端活动中，当前轮次排行榜
	 * @param taskId
	 * @return
	 */
	@Override
	public List<Map<String, Object>> activityCurrentRankingRotation(String taskId) {
		logger.info("获取taskId的值:{}", taskId);
		List<Map<String, Object>> mapList = new ArrayList<>();
		if (StringUtils.isEmpty(taskId)) {
			logger.error("红包信息,taskId不能为空或者不能为null", taskId);
			return mapList;
		}
		mapList = redPacketListDao.activityCurrentRankingRotation(taskId);
		return mapList;
	}

	/**
	 * 屏幕WEB端活动中，本轮中奖纪录
	 * @param map
	 * @return
	 */
	@Override
	public List<RedPacketListVO> allWinningRecords (Map<String, Object> map) {
		Object task = map.get("taskId");
		String taskId = task == null ? null : task.toString();
		logger.info("获取taskId：{}", taskId);
		List<RedPacketListVO> redPacketListVOList = new ArrayList<>();
        if (StringUtils.isEmpty(taskId)) {
            logger.error("红包信息,taskId不能为空或者不能为null:{}", taskId);
            return redPacketListVOList;
        }
        redPacketListVOList = redPacketListDao.allWinningRecords(map);
        return redPacketListVOList;
	}

	/**
	 * 屏幕WEB端活动中，本轮中奖纪录总条数
	 * @param map
	 * @return
	 */
	@Override
	public int allWinningRecordsCount(Map<String, Object> map) {
		return redPacketListDao.allWinningRecordsCount(map);
	}

	/**
	 * 管理后台WEB端，中奖纪录
	 * @param map
	 * @return
	 */
	@Override
	public List<RedPacketListVO> theWinningRecord (Map<String, Object> map) {
		return redPacketListDao.theWinningRecord(map);
	}

	/**
	 * 管理后台WEB端，中奖纪录总条数
	 * @param map
	 * @return
	 */
	@Override
	public int theWinningRecordCount(Map<String, Object> map) {
		return redPacketListDao.theWinningRecordCount(map);
	}

	/**
	 * 管理后台WEB端中奖统计，列表
	 * @param map
	 * @return
	 */
	@Override
	public List<RedPacketListVO> theWinningStatistics (Map<String, Object> map) {
		return redPacketListDao.theWinningStatistics(map);
	}

	/**
	 * 管理后台WEB端中奖统计，总条数
	 * @param map
	 * @return
	 */
	@Override
	public int theWinningStatisticsCount(Map<String, Object> map) {
		return redPacketListDao.theWinningStatisticsCount(map);
	}

	/**
	 * 管理后台WEB端中奖统计，平台分类红包金额和红包个数
	 * @param params
	 * @return
	 */
	@Override
    public List<Map<String, Object>> categorizationNumberAmount(Map<String, Object> params){
		/*String themeName = (String) params.get("themeName");
		logger.info("获取themeName的值:{}", themeName);
		List<Map<String, Object>> mapList = new ArrayList<>();
		if (StringUtils.isEmpty(themeName)) {
			logger.error("红包信息,themeName不能为空或者不能为null", themeName);
			return mapList;
		}
		mapList = redPacketListDao.categorizationNumberAmount(params);*/
		return redPacketListDao.categorizationNumberAmount(params);
    }
}
